package com.micro.ai.commons.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 统一API响应模型
 * 
 * @param <T> 数据类型
 * @author micro-ai
 * @since 0.0.1
 */
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@Schema(description = "统一API响应")
public record ApiResponse<T>(
    @Schema(description = "响应码（200-成功，其他-失败）", example = "200")
    String code,
    
    @Schema(description = "响应消息", example = "操作成功")
    String message,
    
    @Schema(description = "响应数据（泛型）")
    T data,
    
    @Schema(description = "时间戳", example = "2025-10-25 17:00:00")
    LocalDateTime timestamp,
    
    @Schema(description = "请求ID（用于追踪）", example = "req_1234567890")
    String requestId
) implements Serializable {

    /**
     * 成功响应
     */
    public static <T> ApiResponse<T> success() {
        return ApiResponse.<T>builder()
                .code("200")
                .message("操作成功")
                .timestamp(LocalDateTime.now())
                .build();
    }
    
    /**
     * 成功响应
     */
    public static <T> ApiResponse<T> success(T data) {
        return ApiResponse.<T>builder()
                .code("200")
                .message("操作成功")
                .data(data)
                .timestamp(LocalDateTime.now())
                .build();
    }

    /**
     * 成功响应带消息
     */
    public static <T> ApiResponse<T> success(String message, T data) {
        return ApiResponse.<T>builder()
                .code("200")
                .message(message)
                .data(data)
                .timestamp(LocalDateTime.now())
                .build();
    }

    /**
     * 成功响应（无数据）
     */
    public static ApiResponse<Void> successVoid(String message) {
        return ApiResponse.<Void>builder()
                .code("200")
                .message(message)
                .timestamp(LocalDateTime.now())
                .build();
    }

    /**
     * 成功响应带请求ID
     */
    public static <T> ApiResponse<T> success(T data, String requestId) {
        return ApiResponse.<T>builder()
                .code("200")
                .message("操作成功")
                .data(data)
                .timestamp(LocalDateTime.now())
                .requestId(requestId)
                .build();
    }

    /**
     * 失败响应
     */
    public static <T> ApiResponse<T> error(String code, String message) {
        return ApiResponse.<T>builder()
                .code(code)
                .message(message)
                .timestamp(LocalDateTime.now())
                .build();
    }

    /**
     * 失败响应带数据
     */
    public static <T> ApiResponse<T> error(String code, String message, T data) {
        return ApiResponse.<T>builder()
                .code(code)
                .message(message)
                .data(data)
                .timestamp(LocalDateTime.now())
                .build();
    }

    /**
     * 失败响应带请求ID
     */
    public static <T> ApiResponse<T> error(String code, String message, String requestId) {
        return ApiResponse.<T>builder()
                .code(code)
                .message(message)
                .timestamp(LocalDateTime.now())
                .requestId(requestId)
                .build();
    }

    /**
     * 判断是否成功
     */
    public boolean isSuccess() {
        return "200".equals(this.code);
    }

    /**
     * 判断是否失败
     */
    public boolean isError() {
        return !isSuccess();
    }
}
